import {
    validate,
    Length
} from 'class-validator'

import { isError } from 'lodash'

import { Wallet } from '@app/wallet'
import { nothrow } from '@pkg/promise'
import * as utils from '@app/methods/utils'

class Request {
    @Length(34, 34)
    address: string = ''

    @Length(3, 8)
    symbol: string = 'TRX'
}

/**
 * 获取地址代币余额
 */
export async function getBalance(wallet: Wallet, params: any, callback: any) {
    const model = utils.makeModel(new Request, params)
    const errors = await validate(model)
    if (errors.length > 0) {
        callback(utils.BadRequest2(errors), undefined)
        return
    }

    const balance = await nothrow(wallet.getBalance(model.address, model.symbol))
    if (isError(balance)) {
        callback({code: -32603, message: balance.message}, undefined)
        return
    }
    callback(undefined, balance)
}
